package base.thread;

public class SynchronizedClassDemo {

    public void method01() {

        synchronized (SynchronizedClassDemo.class) {
            int i = 5;
            while (i-- > 0) {
                System.out.println("method01                " + Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ie) {
                }
            }
        }
    }

    public void method02() {

        synchronized (this) {
            int i = 5;
            while (i-- > 0) {
                System.out.println("method02                " + Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ie) {
                }
            }
        }
    }

    public static void main(String[] args) {
        final SynchronizedClassDemo myt2 = new SynchronizedClassDemo();
        final SynchronizedClassDemo thread = new SynchronizedClassDemo();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                myt2.method01();
            }
        }, "t1");

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                thread.method02();
            }
        }, "t2");


        t2.start();
        t1.start();

        try {
            t2.join();
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
